﻿using Furion;
using Microsoft.Extensions.Primitives;
using System.Collections.Generic;
using YFurion.Extensions;
using YFurion.Session.Extensions;

namespace YFurion.Session
{
    public class YSession : IYSession
    {
        private long _subjectId;
        public long SubjectId
        {
            get
            {
                if (_subjectId <= 0)
                {
                    string subjectIdStr = this.GetClaimValue(YClaimTypes.SubjectId) ?? "0";
                    long.TryParse(subjectIdStr, out _subjectId);
                }
                return _subjectId;
            }
            set
            {
                _subjectId = value;
            }
        }

        public string Account { get; set; }

        public string UserName { get; set; }

        private string _culture;
        public string Culture
        {
            get
            {
                if (_culture.IsNullOrWhiteSpace())
                {
                    StringValues values = new StringValues();
                    App.HttpContext?.Request.Query.TryGetValue("culture", out values);
                    if (values.Count > 0)
                    {
                        _culture = values[0];
                    }
                }
                return _culture;
            }
            set
            {
                _culture = value;
            }
        }

        public IDictionary<string, string> TableSuffixDictionary { get; }

        /// <summary>
        /// 构造函数
        /// </summary>
        public YSession()
        {
            TableSuffixDictionary = new Dictionary<string, string>();
        }
    }
}
